package org.song.videoplayer.floatwindow;

import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.StackLayout;
import ohos.agp.window.service.Window;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;
import ohos.sysappcomponents.settings.AppSettings;
import org.song.videoplayer.Util;

import java.util.Optional;


/**
 * Created by song on 2018/1/15.
 * Contact github.com/tohodog
 * 视频进出浮窗辅助类
 */

public class FloatWindowHelp implements FloatMoveView.MoveListener {

    private WindowManage windowManage;
    private ComponentContainer decorView;
    private FloatMoveView floatMoveView;
    private FloatParams floatParams, newFloatParams;
    private Context context;
    private int type;

    public FloatWindowHelp(Context context) {
        this.context = context;
        type = WindowManager.LayoutConfig.MOD_APPLICATION_OVERLAY;
        Window window = (Util.scanForActivity(context)).getWindow();
        Optional<Component> focus = window.getCurrentComponentFocus();
        if (focus.isPresent()){
            Component component = focus.get();
            decorView = (ComponentContainer) component.getComponentParent();
        }
    }

    public boolean enterWindowFloat(Component view, final FloatParams floatParams) {
        if (floatParams.systemFloat && !checkPermission()) {
            return false;
        }
        this.floatParams = floatParams;
        newFloatParams = floatParams.clone();
        //拦截触摸容器view
        floatMoveView = new FloatMoveView(context);
        floatMoveView.setMoveListener(this);
//        floatMoveView.setRount(floatParams.round);
        floatMoveView.setAlpha(floatParams.fade);
        floatMoveView.addComponent(view, new StackLayout.LayoutConfig(-1, -1));
        //添加进浮窗
        if (floatParams.systemFloat)
            getWindowManage().addWindowView(floatMoveView);
        else {
            StackLayout.LayoutConfig l = new StackLayout.LayoutConfig(floatParams.w, floatParams.h);
            l.setMarginLeft((decorView.getWidth() - floatParams.w) / 2 + floatParams.x);
            l.setMarginTop((decorView.getHeight() - floatParams.h) / 2 + floatParams.y);
            decorView.addComponent(floatMoveView, l);
        }

        return true;
    }

    public void quieWindowFloat() {
        if (floatMoveView != null) {
            if (floatParams.systemFloat)
                getWindowManage().removeWindowView(floatMoveView);
            else {
                ComponentContainer vp = (ComponentContainer) floatMoveView.getComponentParent();
                if (vp != null)
                    vp.removeComponent(floatMoveView);
            }
            floatMoveView.removeAllComponents();
            floatMoveView = null;

            newFloatParams = null;
            windowManage = null;
        }
    }

    @Override
    public void move(int x, int y) {
        if (floatMoveView != null && floatParams.canMove) {

            newFloatParams.x = floatParams.x + x;
            newFloatParams.y = floatParams.y + y;

            int w = decorView.getWidth();
            int h = decorView.getHeight();

            if (floatParams.systemFloat)//系统浮窗不能超出边界
                getWindowManage().updateWindowView(floatMoveView);
            else {
                ComponentContainer.LayoutConfig l = floatMoveView.getLayoutConfig();
                l.setMarginLeft((w - floatParams.w) / 2 + newFloatParams.x);
                l.setMarginTop((h - floatParams.h) / 2 + newFloatParams.y);
                floatMoveView.setLayoutConfig(l);
            }
        }
    }

    @Override
    public void end() {
        if (floatMoveView == null)
            return;
        floatParams = newFloatParams.clone();
        //界面内浮窗超出边界回弹动画
        if (!floatParams.systemFloat & !floatParams.canCross) {
            final int w = decorView.getWidth();
            final int h = decorView.getHeight();
            int maxLeft = w - floatParams.w;
            int maxTop = h - floatParams.h;
            int newLeft = -1;
            int newTop = -1;
            //判断是否超出边界,以及超出边界后回弹新位置的坐标
            final ComponentContainer.LayoutConfig l = floatMoveView.getLayoutConfig();
            if (l.getMarginLeft() < 0) newLeft = 0;
            if (l.getMarginLeft() > maxLeft) newLeft = maxLeft;
            if (l.getMarginTop() < 0) newTop = 0;
            if (l.getMarginTop() > maxTop) newTop = maxTop;
            if (newLeft == -1 & newTop == -1)
                return;
            //执行属性动画
            AnimatorProperty animator = new AnimatorProperty();
            animator.setTarget(floatMoveView);
            final int finalNewLeft = newLeft;
            final int finalNewTop = newTop;
            final int finalLeft = l.getMarginLeft();
            final int finalTop = l.getMarginTop();
            if (finalNewLeft >= 0)
                l.setMarginLeft(finalLeft + finalNewLeft - finalLeft);
            if (finalNewTop >= 0)
                l.setMarginTop(finalTop + finalNewTop - finalTop);
            floatMoveView.setLayoutConfig(l);
            //更新浮窗中心点坐标
            floatParams.x = l.getMarginLeft() + floatParams.w / 2 - w / 2;
            floatParams.y = l.getMarginTop() + floatParams.h / 2 - h / 2;
            animator.moveToX(floatParams.x);
            animator.moveByY(floatParams.y);
            animator.setDuration(300).start();
            // animator.setInterpolator(value)
        }

    }

    public FloatParams getFloatParams() {
        return floatParams;
    }


    private WindowManage getWindowManage() {
        if (windowManage == null)
            windowManage = new WindowManage(context);
        return windowManage;
    }

    //检查浮窗权限
    public boolean checkPermission() {
        return AppSettings.canShowFloating(context);
    }
}

